<template>
  <div class="login">
    <common-head title="登录页"></common-head>
    <div class="warp">
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="user.pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
        <van-button
        @click="$router.push('/regist')"
         type="danger" round  size="large">注册</van-button>
      </div>
    </van-form>
    </div>
  </div>
</template>

<script>
import CommonHead from '../../components/CommonHead.vue'
import { fetchLogin } from 'api'
import { Toast } from 'vant'
export default {
  name: 'login',
  data () {
    return {
      user: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      params = {
        ...params,
        deviceId: 2103,
        deviceName: '我的'
      }
      fetchLogin(params).then(res => {
        if (res.data.code === 0) {
          localStorage.setItem('token', res.data.data.token)
          Toast.success({
            message: '登录成功',
            duration: 1000,
            onClose: () => {
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from)
              } else {
                this.$router.replace('/home')
              }
            }
          })
        } else {
          Toast.fail('登录失败')
        }
      })
    }
  },
  components: { CommonHead }

}
</script>

<style lang="scss" scoped>
.login{
  width: 100%;
  height: 667.2px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('./1.jpg');
  .warp{
  padding-top: 46px;
  /deep/.van-form{
    width: 350px;
    .van-cell{
      background: rgba(0,0,0,0);
    }
    .van-field__body{
      line-height: 48px;
    }
    .van-button{
      margin-top: 10px;
    }
  }
}
}
/deep/.van-field__label{
  line-height: 48px;
  text-align: center;
      color: #fff;
    }
</style>
